#include "changesinfo.h"
#include "ui_changesinfo.h"
#include<QSqlQuery>
#include<QMessageBox>
#include<QSqlRecord>
#include"formS.h"
#include"studentinfo.h"
#include"student.h"

changeSinfo::changeSinfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::changeSinfo)
{
    ui->setupUi(this);
}

changeSinfo::~changeSinfo()
{
    delete ui;
}

 void changeSinfo::showchangeSinfo()
 {
     emit signalchangeSinfo();
     this->show();
 }

void changeSinfo::on_pb_ok1_clicked()
{
    FormS forms;
    StudentInfo stu_info = forms.sinfo;
    QString id = forms.id;
    QString sex = ui->lineEdit_Sex->text();
    QString name = ui->lineEdit_Name->text();
    QString birth = ui->lineEdit_Birth->text();
    QSqlQuery query;
    QString queryString =
            QString("update StudentInfoTable set Sex="+sex +", Name = "+
                    name+",Birth = "+birth+"where ID = '%1'").arg(id);
    if(query.exec(queryString))
    {
        int ID_id = query.record().indexOf("ID");
        int xuehao_id = query.record().indexOf("xuehao");
        int Name_id = query.record().indexOf("Name");
        int Sex_id = query.record().indexOf("Sex");
        int Classid_id = query.record().indexOf("Classid");
        int loginpw_id = query.record().indexOf("loginpw");
        int RxTime_id = query.record().indexOf("RxTime");

        while(query.next())
        {
            QString id = query.value(ID_id).toString();
            QString Name = query.value(Name_id).toString();
            QString XuehaoID = query.value(xuehao_id).toString();
            QString pswd = query.value(loginpw_id).toString();
            QString Classid = query.value(Classid_id).toString();
            QString Sex = query.value(Sex_id).toString();
            QString rxtime = query.value(RxTime_id).toString();
             stu_info.updateStudentInfo(id, Name,XuehaoID, pswd,Sex, Classid, pswd);
             stu_info.display();
          }
        }else{
            QMessageBox msgBox;
            msgBox.setText("Failed:updata people !!");
            msgBox.setInformativeText("Please try again !");
            msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
            msgBox.setDefaultButton(QMessageBox::Retry);

            int ret = msgBox.exec();
            switch (ret) {
            case QMessageBox::Retry:
              ui->lineEdit_Sex->setFocus();
              break;
            case QMessageBox::Cancel:
              this->close();
              break;
            default:
              // should never be reached
              break;
            }
          }
}

void changeSinfo::showSW()
{
    Student *sw = new Student;
    sw->show();
    sw->selectBoxSlot(str);
}

void changeSinfo::on_pb_cancel1_clicked()
{
    this->close();
    this->showSW();
}
